package ltd.sshine.websocket.Util;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import ltd.sshine.websocket.DO.Message;
import org.springframework.stereotype.Component;
import java.util.Date;


@Component
public class MessageUtil {


 private static final ObjectMapper objectMapper=new ObjectMapper();


    public String toString(Message msg){

        try {
            msg.setTime(getdate())
                .setCode(1);
            if (msg.getData()==null){

                msg.setData("");
            }
            return objectMapper.writeValueAsString(msg);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return null;
    }

    public Message getMsgObj(String msg){


        try {
            return objectMapper.readValue(msg,Message.class);
        } catch (JsonProcessingException e) {
          return null;
        }


    }

    public String getMsg(String type,String data){

        return toString(new Message().setType(type).setData(data));


    }
    public String Msgerr(String data){
        return toString(new Message().setType("err").setData(data).setCode(0));
    }
    public String Msgtip(String data,int code){

        return toString(new Message().setType("tip").setData(data).setCode(code));


    }

    private Date getdate(){
        return new Date();
    }





}
